package com.lifeverse.entity;

import com.lifeverse.entity.enums.AlertSeverity;
import com.lifeverse.entity.enums.AlertStatus;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 系统告警实体
 */
@Entity
@Table(name = "system_alerts")
@Data
@EqualsAndHashCode(callSuper = true)
public class SystemAlert extends BaseEntity {

    /**
     * 告警名称
     */
    @Column(nullable = false, length = 200)
    private String alertName;

    /**
     * 告警描述
     */
    @Column(columnDefinition = "TEXT")
    private String description;

    /**
     * 告警严重程度
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private AlertSeverity severity;

    /**
     * 告警状态
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private AlertStatus status;

    /**
     * 告警源（服务名称或组件名称）
     */
    @Column(nullable = false, length = 100)
    private String source;

    /**
     * 告警规则名称
     */
    @Column(length = 100)
    private String ruleName;

    /**
     * 告警触发时间
     */
    @Column(nullable = false)
    private LocalDateTime triggeredAt;

    /**
     * 告警确认时间
     */
    private LocalDateTime acknowledgedAt;

    /**
     * 告警解决时间
     */
    private LocalDateTime resolvedAt;

    /**
     * 确认人
     */
    @Column(length = 100)
    private String acknowledgedBy;

    /**
     * 解决人
     */
    @Column(length = 100)
    private String resolvedBy;

    /**
     * 告警标签（JSON格式存储）
     */
    @ElementCollection
    @CollectionTable(name = "alert_labels", joinColumns = @JoinColumn(name = "alert_id"))
    @MapKeyColumn(name = "label_key")
    @Column(name = "label_value")
    private Map<String, String> labels;

    /**
     * 告警注解（JSON格式存储）
     */
    @ElementCollection
    @CollectionTable(name = "alert_annotations", joinColumns = @JoinColumn(name = "alert_id"))
    @MapKeyColumn(name = "annotation_key")
    @Column(name = "annotation_value", columnDefinition = "TEXT")
    private Map<String, String> annotations;

    /**
     * 告警值
     */
    private Double alertValue;

    /**
     * 阈值
     */
    private Double threshold;

    /**
     * 告警次数
     */
    @Column(nullable = false)
    private Integer alertCount = 1;

    /**
     * 最后更新时间
     */
    @Column(nullable = false)
    private LocalDateTime lastUpdatedAt;

    /**
     * 是否已发送通知
     */
    @Column(nullable = false)
    private Boolean notificationSent = false;

    /**
     * 通知发送时间
     */
    private LocalDateTime notificationSentAt;

    /**
     * 告警持续时间（秒）
     */
    public Long getDurationInSeconds() {
        if (triggeredAt == null) return 0L;
        
        LocalDateTime endTime = resolvedAt != null ? resolvedAt : LocalDateTime.now();
        return java.time.Duration.between(triggeredAt, endTime).getSeconds();
    }

    /**
     * 判断是否为活跃告警
     */
    public boolean isActive() {
        return status == AlertStatus.ACTIVE;
    }

    /**
     * 判断是否需要立即处理
     */
    public boolean requiresImmediateAction() {
        return isActive() && severity.isUrgent();
    }

    /**
     * 确认告警
     */
    public void acknowledge(String acknowledgedBy) {
        this.status = AlertStatus.ACKNOWLEDGED;
        this.acknowledgedBy = acknowledgedBy;
        this.acknowledgedAt = LocalDateTime.now();
        this.lastUpdatedAt = LocalDateTime.now();
    }

    /**
     * 解决告警
     */
    public void resolve(String resolvedBy) {
        this.status = AlertStatus.RESOLVED;
        this.resolvedBy = resolvedBy;
        this.resolvedAt = LocalDateTime.now();
        this.lastUpdatedAt = LocalDateTime.now();
    }

    /**
     * 增加告警次数
     */
    public void incrementAlertCount() {
        this.alertCount++;
        this.lastUpdatedAt = LocalDateTime.now();
    }
}